package com.web.test.dao;

import com.web.test.bean.Question;
import com.web.test.utils.Constants;
import com.web.test.utils.JDBCUtil;
import com.web.test.utils.SQLUtils;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/23.
 */
public class QuestionDao {

    public List<Question> find(String sql)
            throws ClassNotFoundException, SQLException, IOException {

        Connection connection = JDBCUtil.getConnection();
        Statement statement = connection.createStatement();
        ResultSet rSet = statement.executeQuery(sql);

        List<Question> msList = getQuestionsByRS(rSet);

        JDBCUtil.close(connection, statement, rSet);

        return msList;
    }

    public Map<Integer, List<Question>> getOneQuestion() throws SQLException, IOException, ClassNotFoundException {

        Map<Integer, List<Question>> map = new HashMap<Integer, List<Question>>();

        List<Question> only = find(SQLUtils.onlySelectAll);
        List<Question> multi = find(SQLUtils.multiSelectAll);
        List<Question> judge = find(SQLUtils.judgeAll);
        List<Question> write = find(SQLUtils.writeAll);

        map.put(Constants.ONLY_SELECT_Q, getQ(only, 3));
        map.put(Constants.MULTI_SELECT_Q, getQ(multi, 2));
        map.put(Constants.JUDGE_Q, getQ(judge, 2));
        map.put(Constants.WRITE_Q, getQ(write, 2));

        return map;
    }

    private List<Question> getQ(List<Question> oldQ, int randomCount) {

        List<Question> newQ = new ArrayList<Question>();

        int[] result = new int[randomCount];
        int count = 0;
        while (count < randomCount){

            int temp = (int) ((Math.random()*(oldQ.size()))+1);
            boolean flag = true;
            for (int i = 0; i < randomCount; i++) {
                if (temp == result[i]){
                    flag = false;
                    break;
                }
            }

            if (flag){
                result[count] = temp;
                count++;
                newQ.add(oldQ.get(temp-1));
            }

        }

        return newQ;
    }


    private List<Question> getQuestionsByRS(ResultSet rSet) throws SQLException {

        List<Question> msList = new ArrayList<Question>();

        while (rSet.next()) {


            String no = rSet.getString("QUESTION_NO");
            String title = rSet.getString("QUESTION_TITLE");
            String options = rSet.getString("OPTIONS");
            int type = rSet.getInt("TYPE");
            int score = rSet.getInt("SCORE");
            List<String> op = new ArrayList<String>();

            if (options != null) {


                String[] strings = options.split("，");

                for (int i = 0; i < strings.length; i++) {
                    op.add(strings[i]);
                }

            }

            Question question = new Question(no, title, op, type, score);
            msList.add(question);
        }
        return msList;
    }


}
